package cn.itcast.n4_共享模型_管程;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

import static cn.itcast.util.Sleeper.sleep;

//线程1:a
//线程2:b
//线程3:c

//线程1:a
//线程2:b
//线程3:c

//线程1:a
//线程2:b
//线程3:c
// 等待标记      输出内容   修改标记为
//    1            a         2
//    2            b         3
//    3            c         1
@Slf4j(topic = "c.Test50_$$$$$_设计模式之顺序控制_park_unpark实现3个线程交替输出abc五次")
public class Test50_$$$$$_设计模式之顺序控制_park_unpark实现3个线程交替输出abc五次 {
    static Thread a;
    static Thread b;
    static Thread c;
    public static void main(String[] args) {
        ParkUnpark1 parkUnpark1 = new ParkUnpark1(5);

        a = new Thread(() -> {
            parkUnpark1.print("a", b);
        }, "a");

        b = new Thread(() -> {
            parkUnpark1.print("b", c);
        }, "b");

        c = new Thread(() -> {
            parkUnpark1.print("c", a);
        }, "c");
        a.start();
        b.start();
        c.start();

        sleep(1);
        LockSupport.unpark(a);


    }
}

@Slf4j(topic = "c.ParkUnpark1")
class ParkUnpark1 {
    private int loopNumber;

    public ParkUnpark1(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public void print(String printData,Thread notifyThread){
        for (int i = 0; i < loopNumber; i++) {
            LockSupport.park();
            System.out.print(printData);
            LockSupport.unpark(notifyThread);
        }
    }
}
